/*
 * Copyright (c) 2008 VMware, Inc.
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx;

import wtkx.collections.ArrayList;
import wtkx.collections.Sequence;
import wtkx.util.ImmutableIterator;
import wtkx.util.ListenerList;
import wtkx.wtk.content.MenuBarItemDataDecorator;

import java.util.Iterator;

/**
 * Component representing a {@link MenuBar} item.
 *
 * @author gbrown
 */
public class MenuBarItem 
    extends Button 
{
    private static final Button.DataDecorator DEFAULT_DATA_RENDERER = new MenuBarItemDataDecorator();

    public class ItemListenerList
        extends ListenerList<MenuBar.ItemListener>
        implements MenuBar.ItemListener 
    {
        ItemListenerList(){
            super();
        }

        public void menuChanged(MenuBarItem item, Menu previousMenu) {
            for (MenuBar.ItemListener listener : this) {
                listener.menuChanged(item, previousMenu);
            }
        }
    }

    private MenuBar menuBar = null;
    private Menu menu = null;

    private ItemListenerList itemListeners = new ItemListenerList();

    public MenuBarItem() {
        this(null);
    }

    public MenuBarItem(Object buttonData) {
        super(buttonData);

        this.setDataDecorator(DEFAULT_DATA_RENDERER);
        this.installSkin(MenuBarItem.class);
    }

    @Override
    public void setParent(Container parent) {
        if (!(parent instanceof MenuBar)) {
            throw new IllegalArgumentException("Parent must be an instance of "
                                               + MenuBar.class.getName());
        }

        super.setParent(parent);
    }

    public MenuBar getMenuBar() {
        return menuBar;
    }

    public void setMenuBar(MenuBar menuBar) {
        this.menuBar = menuBar;
    }

    public Menu getMenu() {
        return menu;
    }

    public void setMenu(Menu menu) {
        if (menu != null
            && menu.getItem() != null) {
            throw new IllegalArgumentException("menu already belongs to an item.");
        }

        Menu previousMenu = this.menu;

        if (previousMenu != menu) {
            this.menu = menu;
            itemListeners.menuChanged(this, previousMenu);
        }
    }

    @Override
    public void setToggleButton(boolean toggleButton) {
        throw new UnsupportedOperationException("Menu bar items cannot be toggle buttons.");
    }

    public ListenerList<MenuBar.ItemListener> getItemListeners() {
        return itemListeners;
    }
}
